package com.cw.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cw.config.JwtConfig;
import com.cw.entity.Orders;
import com.cw.entity.RunnerOrder;
import com.cw.service.OrdersService;
import com.cw.service.RunnerOrderService;
import com.cw.mapper.RunnerOrderMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
* @author BerNardShaw
* @description 针对表【runner_order】的数据库操作Service实现
* @createDate 2022-10-30 21:10:12
*/
@Service
public class RunnerOrderServiceImpl extends ServiceImpl<RunnerOrderMapper, RunnerOrder>
    implements RunnerOrderService{
    @Resource
    private OrdersService ordersService;
    @Resource
    private RedisTemplate redisTemplate;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean getOrder(RunnerOrder dto, HttpServletRequest request) {
//        dto.setCreateTime(new Date());
//        dto.setCreateBy(String.valueOf(JwtConfig.getCurrentId(request)));
//        dto.setUpdateBy(String.valueOf(JwtConfig.getCurrentId(request)));
        dto.setRunnerId(Integer.valueOf(JwtConfig.getCurrentId(request).toString()));
        boolean save = this.save(dto);
        LambdaUpdateWrapper<Orders> ordersLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        LambdaUpdateWrapper<Orders> wrapper = ordersLambdaUpdateWrapper.set(save, Orders::getStatus, 3).eq(Orders::getId, dto.getOrderId());
        boolean update = this.ordersService.update(wrapper);
        return save&&update;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean confirmOrder(RunnerOrder dto, HttpServletRequest request) {
        LambdaUpdateWrapper<Orders> ordersLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        LambdaUpdateWrapper<RunnerOrder> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        LambdaUpdateWrapper<Orders> wrapper = ordersLambdaUpdateWrapper.set(Orders::getStatus, 4)
                .eq(Orders::getId, dto.getOrderId());
        LambdaUpdateWrapper<RunnerOrder> wrapper1 = lambdaUpdateWrapper.set(RunnerOrder::getArrivedTime, new Date())
                .eq(RunnerOrder::getOrderId, dto.getOrderId())
                .eq(RunnerOrder::getRunnerId, JwtConfig.getCurrentId(request));
        boolean update = this.ordersService.update(wrapper);
        boolean update1 = this.update(wrapper1);
        return update1&&update;
    }
}




